package com.yyk.yykywd.ui.work;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.base.BaseRecyclerAdapter;
import com.yyk.yykywd.base.BaseRecyclerHolder;
import com.yyk.yykywd.config.Constant;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.bean.WorkPlandetailInfo;
import com.yyk.yykywd.listenter.DialogSureCancelListenter;
import com.yyk.yykywd.listenter.ToolbarListenter;
import com.yyk.yykywd.util.DialogUtil;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.OnClickFlagUtil;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 业务计划 详情
 */
public class BusinessPlan2Activity extends BaseActivity {

    public static BusinessPlan2Activity intance;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_createtime)
    TextView tvCreatetime;
    @BindView(R.id.tv_topname)
    TextView tvTopname;
    @BindView(R.id.tv_time_end)
    TextView tvTimeEnd;
    @BindView(R.id.tv_goal)
    TextView tvGoal;
    @BindView(R.id.tv_rests)
    TextView tvRests;
    @BindView(R.id.rv_udate)
    RecyclerView rvUdate;
    @BindView(R.id.bt_customer)
    Button btCustomer;
    @BindView(R.id.tv_name)
    TextView tvName;
    //
    private String id; //业务ID
    private String number;
    private String createtime;
    private String topname;
    private String name;

    @Override
    public int getLayoutId() {
        return R.layout.activity_business_plan2;
    }

    @Override
    public void initView() {
        setTitle("业务计划");
        setBackArrow();
        setMainTitleRightText("删除计划", new ToolbarListenter() {
            @Override
            public void onClickListenter() {
                Bundle bundle = new Bundle();
                bundle.putString("id", id);
                startAct(DeletePlanActivity.class, bundle);
            }
        });
    }

    @Override
    public void initData() {
        intance = this;
        initGetIntent();
        initPost();
    }

    private void initPost() {
        postWorkPlandetail(true);
    }

    /**
     * 接口4：查看业务详情（whm）
     *
     * @param flag
     */
    private void postWorkPlandetail(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid")+"");
        map.put("bid", id + "");
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postWorkPlandetail(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<WorkPlandetailInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(WorkPlandetailInfo info) {
                        Logger.e("查看业务详情" + info.toString());
                        if (info.getCode() == 1) {

                            WorkPlandetailInfo.DataBean.PlanBean plan = info.getData().getPlan();
                            //业务编号
                            number = plan.getNumber();
                            //创建时间
                            createtime = plan.getCreatetime();
                            //业务类型
                            topname = plan.getTopname();
                            //机构名称
                            name = plan.getName();
                            tvCreatetime.setText(createtime);
                            tvGoal.setText(plan.getGoal());
                            tvNumber.setText(number);
                            tvRests.setText(plan.getRests());
                            if (plan.getEnd() == 1) {
                                tvTimeEnd.setText(plan.getTime() + "   上午");
                            } else if (plan.getEnd() == 2) {
                                tvTimeEnd.setText(plan.getTime() + "   下午");
                            } else {
                                tvTimeEnd.setText(plan.getTime() + "   晚上");
                            }
                            tvTopname.setText(topname);
                            tvName.setText(name);
                            List<WorkPlandetailInfo.DataBean.PlanBean.UdataBean> udata = plan.getUdata();
                            BaseRecyclerAdapter<WorkPlandetailInfo.DataBean.PlanBean.UdataBean> adapter = new BaseRecyclerAdapter<WorkPlandetailInfo.DataBean.PlanBean.UdataBean>(mActivity, udata, R.layout.adapter_item_businessplan_deteail) {
                                @Override
                                public void convert(BaseRecyclerHolder holder, WorkPlandetailInfo.DataBean.PlanBean.UdataBean item, int position, boolean isScrolling) {
                                    holder.setImageByUrl(R.id.iv_img, item.getImg());
                                    holder.setText(R.id.tv_uname, item.getUname());
                                }
                            };
                            rvUdate.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false));
                            rvUdate.setAdapter(adapter);

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initGetIntent() {
        if (getBundle() != null) {
            id = getBundle().getString("id");
        }
    }


    @OnClick({R.id.bt_customer})
    public void onViewClicked(View view) {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.bt_customer:
                DialogUtil.showReturnTips(mActivity, "",
                        "进行客户拜访，填写机构联系人相关信息后，可进行后续业务流程"

                        , "确定", "取消", new DialogSureCancelListenter() {
                            @Override
                            public void onSureListenter() {
                                Bundle bundle = new Bundle();
                                bundle.putString("id", id);

                                startAct(CustomerCall2Activity.class, bundle);
                            }

                            @Override
                            public void onCancelListenter() {

                            }
                        }, false);

                break;

        }


    }


}
